package com.asimplefox.controllers;

import com.asimplefox.entity.Role;
import com.asimplefox.entity.VidList;
import com.asimplefox.service.IRoleService;
import com.asimplefox.service.IUserService;
import com.asimplefox.service.IVidListService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <br/> 后台管理需要
 * <br/>可视化控制台中心：订单成交金额，网站访问量（去重），在线用户量，总用户量
 * <br/>短剧：短剧数量，已完结数量，连载数量
 * <br/>国漫：国漫数量，已完结数量，连载数量，
 * <br/>短剧播放量排名，国漫播放量排名，网站会员数量
 * <br/>网站用户来源显示访问量，按设备类型显示访问次数
 *
 * @author github kloping
 * @date 2024/12/01-13:59
 */
@RestController
@RequestMapping("/api/counts")
public class CountsController {

    //session id  2 cd
    private Map<String, Long> alives = new HashMap<>();

    @Scheduled(cron = "0 0/1 * * * ?")
    private void testCd() {
        alives.entrySet().removeIf(entry -> System.currentTimeMillis() - entry.getValue() > 2 * 60 * 1000);
    }

    @RequestMapping("/keepalive")
    public Object keepalive(HttpServletRequest request) {
        String sessionId = request.getSession().getId();
        alives.put(sessionId, System.currentTimeMillis());
        return "ok";
    }

    @Autowired
    IUserService userService;

    @RequestMapping("/all/users")
    public Object allUsers() {
        return userService.count();
    }

    @RequestMapping("/live/users")
    public Object liveUsers() {
        return alives.size();
    }

    @Autowired
    IRoleService roleService;

    @RequestMapping("/user/vips")
    public Object userVips() {
        return roleService.lambdaQuery().eq(Role::getUserRole, "vip")
                .gt(Role::getTimestamp, System.currentTimeMillis()).count();
    }

    @Autowired
    IVidListService vidListService;

    //全部的
    @RequestMapping("/vid/all")
    public Object allvid() {
        return vidListService.lambdaQuery().eq(VidList::getFtype, 0).count();
    }

    //完结的
    @RequestMapping("/vid/fin")
    public Object allfin() {
        return vidListService.lambdaQuery().eq(VidList::getFtype, 0).eq(VidList::getType, "完结").count();
    }

    //连载的的
    @RequestMapping("/vid/ufin")
    public Object allufin() {
        return vidListService.lambdaQuery().eq(VidList::getFtype, 0).eq(VidList::getType, "连载").count();
    }

    //国漫的
    @RequestMapping("/gm/all")
    public Object allgm() {
        return vidListService.lambdaQuery().eq(VidList::getFtype, 1).count();
    }

    //完结的
    @RequestMapping("/gm/fin")
    public Object allgmfin() {
        return vidListService.lambdaQuery().eq(VidList::getFtype, 1).eq(VidList::getType, "完结").count();
    }

    //连载的的
    @RequestMapping("/gm/ufin")
    public Object allgmufin() {
        return vidListService.lambdaQuery().eq(VidList::getFtype, 1).eq(VidList::getType, "连载").count();
    }
}
